﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxPro;

using CMS.Core.DataInterfaces;
using CMS.Data;
using CMS.Core.Domain;
using CMS.Presenters.Presenters;
using CMS.Presenters.Views;
using CMS.Data.Common;

namespace CMS.Web.CompDenu
{
    public partial class AddTiepDan : BaseUserControl, IAddTiepDanView
    {
        private AddTiepDanPresenter presenter;
        // For Nguoi su dung
        IList<LoaiHinh> lhList;
        IList<PBTiepCD> pbList;
        IList<LinhVuc> lvList;
        IList<DiaBan> dbList;
        IList<TrangThaiXuLy> ttxlList;
        IList<NguoiSuDung> nsdList;
        NguoiSuDungInfo userInfo = null;
        String tiepDanID = "";
        TiepDan _tiepDan;
        LanhDao _lanhDao;
        //CongDan _congDan;
        // statDone: -1 - Chưa thực hiện; 1 - Thành công; 0 - Lỗi
        public int statDone = -1;

        public AddTiepDan()
        {
            this.presenter = new AddTiepDanPresenter(this, NHibernateDaoFactory.Instance.GetTiepDanDao());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            userInfo = (NguoiSuDungInfo)Session["UserInfo"];
            tiepDanID = Request.QueryString.Get("objID") == null ? "" : Request.QueryString.Get("objID");
            statDone = Request.QueryString.Get("statDone") == null ? -1 : Convert.ToInt32(Request.QueryString.Get("statDone"));

            this.RaiseClientEvent();
            if (!tiepDanID.Equals(""))
            {
                _tiepDan = TiepDanDao.Instance.GetById(Convert.ToInt32(tiepDanID));
                if (_tiepDan.LanhDaos.Count > 0)
                {
                    _lanhDao = _tiepDan.LanhDaos[0];
                }
                else
                {
                    _lanhDao = new LanhDao();
                    _lanhDao.MaTiepDan = _tiepDan.ID;
                }

                //if (_tiepDan.CongDans.Count > 0)
                //{
                //    _congDan = _tiepDan.CongDans[0];
                //}
                //else
                //{
                //    _congDan = new CongDan();
                //    _congDan.MaTiepDan = _tiepDan.ID;
                //}
            }
            if (!this.IsPostBack)
            {
                this.lblMess.InnerHtml = "";
                if (tiepDanID.Equals(""))
                {
                    this.InitForm();
                }
                else
                {
                    this.lblTitle.InnerHtml = "HIỆU CHỈNH TIẾP CÔNG DÂN";
                    this.presenter.LoadViewFromModel();
                    InitFormEdit(_tiepDan);
                }
                if (statDone != -1)
                {
                    rowMess.Visible = true;
                    this.lblMess.Visible = true;
                    if (statDone == 1)
                    {
                        this.lblMess.InnerHtml = "Cập nhật thành công!";
                    }
                    else if (statDone == 0)
                    {
                        this.lblMess.InnerHtml = "Lỗi cập nhật dữ liệu!";
                    }
                }
                
            }
            // For Ajax
            //this.RaiseClientEvent();

            //this.btnOK.Attributes.Add("onclick", "return CheckInput_callback();");
            //this.btnOKC.Attributes.Add("onclick", "return CheckInput(1);");
        }

        // For Ajax to validdate
        protected void RaiseClientEvent()
        {
            rowMess.Visible = false;
            Utility.RegisterTypeForAjax(typeof(CMS.Presenters.Presenters.AddTiepDanPresenter));
        }

        public string Message
        {
            set { //lblMessage.Text = value; 
            }
        }

        public TiepDan TiepDanToEdit
        {
            get
            {
                return _tiepDan;
            }
            set
            {
                this._tiepDan = value;
            }
        }

        public TiepDan TiepDanToAdd
        {
            get
            {   TiepDan tdObj;

                if (TiepDanToEdit != null)
                {
                    tdObj = TiepDanToEdit;
                }
                else
                {
                    tdObj = new TiepDan();
                }
                    
                tdObj.SoThuLy = Convert.ToInt32(txtSoThuLy.Text.Trim());
                string[] temp = txtNgayTiep.Text.Trim().Split(new Char[] { '/' });
                tdObj.NgayTiep = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                tdObj.LoaiHinhLookup = LoaiHinhDao.Instance.GetById(Convert.ToInt32(drpLoaiHinh.SelectedValue));
                tdObj.PBTiepLookup = PBTiepCDDao.Instance.GetById(Convert.ToInt32(drpPBTiepCD.SelectedValue));
                tdObj.LanTiep = Convert.ToByte(txtLanTiep.Text.Trim());
                tdObj.DiaChi = txtDiaChi.Text.Trim();
                tdObj.LinhVucLookup = LinhVucDao.Instance.GetById(Convert.ToInt32(drpLinhVuc.SelectedValue));
                tdObj.NoiDung = txtTomTat.Text.Trim();
                tdObj.YKLD = txtYKienTiepDan.Text.Trim();
                tdObj.MaDiaBan = Convert.ToInt32(hidDiaBan.Value);
                tdObj.KetQuaGiaiQuyet = txtKetQuaGiaiQuyet.Text.Trim();
                tdObj.BoPhanTiep = Convert.ToInt32(drpBPTiep.SelectedValue);
                tdObj.TenCongDan = txtHoTen.Text.Trim();
                
                return tdObj;
            }
            set
            {
                if (value == null) throw new ArgumentNullException("ObjectToUpdate may not be null");
            }
        }
        
        public LanhDao LanhDaoToAdd
        {
            get
            {
                LanhDao ldObj = LanhDaoToEdit;
                if (LanhDaoToEdit == null)
                {
                    ldObj = new LanhDao();
                }
                ldObj.TenLanhDao = txtLanhDao.Text.Trim();
                return ldObj;
            }
            set
            {
                if (value == null) throw new ArgumentNullException("ObjectToUpdate may not be null");
            }
        }

        public LanhDao LanhDaoToEdit
        {
            get
            {
                return _lanhDao;
            }
            set
            {
                this._lanhDao = value;
            }
        }

        //public CongDan CongDanToAdd
        //{
        //    get
        //    {
        //        CongDan cdObj = CongDanToEdit;
        //        if (cdObj == null)
        //        {
        //            cdObj = new CongDan();
        //        }
        //        cdObj.TenCongDan = txtHoTen.Text.Trim();
        //        return cdObj;
        //    }
        //    set
        //    {
        //        if (value == null) throw new ArgumentNullException("ObjectToUpdate may not be null");
        //    }
        //}

        //public CongDan CongDanToEdit
        //{
        //    get
        //    {
        //        return _congDan;
        //    }
        //    set
        //    {
        //        this._congDan = value;
        //    }
        //}

        // For Dia ban
        public IList<DiaBan> DiaBanList
        {
            get { return this.dbList; }
            set { this.dbList = value; }
        }
        // For Loai hinh
        public IList<LoaiHinh> LoaiHinhList
        {
            get { return this.lhList; }
            set { this.lhList = value; }
        }
        public IList<PBTiepCD> PBTiepCDList
        {
            get { return this.pbList; }
            set { this.pbList = value; }
        }
        // For Linh vuc
        public IList<LinhVuc> LinhVucList
        {
            get { return this.lvList; }
            set { this.lvList = value; }
        }
        // For Trang thai xu ly
        public IList<TrangThaiXuLy> TTXLList
        {
            get { return this.ttxlList; }
            set { this.ttxlList = value; }
        }
        // For Nguoi su dung
        public IList<NguoiSuDung> NguoiSuDungList
        {
            get { return this.nsdList; }
            set { this.nsdList = value; }
        }
        // For Nguoi su dung
        public string getTenNguoiSuDung(string code)
        {
            return presenter.getNguoiSuDung(code).TenNguoiSuDung;
        }

        public String ID
        {
            get { return this.tiepDanID; }
            set { this.ID = value; }
        }

        public int LoaiPhieu
        {
            get { return 1;// drpLoaiPhieu.SelectedIndex; 
            }
        }
        /// <summary>
        /// Save button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            statDone = (presenter.AddObj(Page.IsValid)) ? 1 : 0;
            
            if (presenter.GetTiepDanID() == -1)
            {
                Response.Redirect(BaseUrl + "&action=EDIT&statDone=" + Convert.ToString(statDone) + "&objID=" + ID);
            }
            else
            {
                Response.Redirect(BaseUrl + "&action=EDIT&statDone=" + Convert.ToString(statDone) + "&objID=" + Convert.ToUInt32(presenter.GetTiepDanID()));
            }
            
        }

        protected void btnOKC_Click(object sender, EventArgs e)
        {
            presenter.AddObj(Page.IsValid);
            //this.btnCancel_Click(this, null);
            Response.Redirect(BaseUrl + "&action=EDIT&statDone=" + Convert.ToString(statDone) + "&objID=" + Convert.ToInt32(presenter.GetTiepDanID()));
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(BaseUrl + "&action=VIEW&statDone=" + Convert.ToString(statDone));
        }

        protected void InitForm()
        {
            txtSoThuLy.Text = Convert.ToString(presenter.GetMaxSoThuLy()); ;
            txtNgayTiep.Text = DateTime.Now.ToString("dd/MM/yyyy");
            txtHoTen.Text = "";
            txtLanTiep.Text = "1";
            txtLanhDao.Text = "";
            txtDiaChi.Text = "";
            txtTomTat.Text = "";
            txtYKienTiepDan.Text = "";
            txtKetQuaGiaiQuyet.Text = "";
            hidNguoiXuLy.Value = (userInfo == null) ? "" : userInfo.ID;
            this.drpLoaiHinh.DataSource = this.LoaiHinhList;
            drpLoaiHinh.DataTextField = "TenLoaiHinh";
            drpLoaiHinh.DataValueField = "ID";
            this.drpLoaiHinh.DataBind();
            // For Dia ban
            this.drpDiaBan.ReadOnly = true;
            // For Linh vuc
            this.drpLinhVuc.DataSource = this.LinhVucList;
            drpLinhVuc.DataTextField = "TenLinhVuc";
            drpLinhVuc.DataValueField = "ID";
            this.drpLinhVuc.DataBind();

            //phong ban tiep
            this.drpPBTiepCD.DataSource = this.PBTiepCDList;
            drpPBTiepCD.DataTextField = "TenPB";
            drpPBTiepCD.DataValueField = "ID";
            this.drpPBTiepCD.DataBind();
        }
        
        protected void InitFormEdit(TiepDan obj)
        {
            txtSoThuLy.Text = obj.SoThuLy.ToString();
            //txtSoThuLy.ReadOnly = true;
            txtNgayTiep.Text = obj.NgayTiep.ToString("dd/MM/yyyy");
            //string CongDan = "";
            //for (int i = 0; i < obj.CongDans.Count -1; i++)
            //{
            //    CongDan += obj.CongDans[i].TenCongDan + ",";
            //}
            //if (obj.CongDans.Count > 0)
            //    CongDan += obj.CongDans[obj.CongDans.Count - 1].TenCongDan;
            //txtHoTen.Text = CongDan;// (obj.CongDans.Count > 0) ? obj.CongDans[0].TenCongDan : "";
            txtHoTen.Text = obj.TenCongDan;
            txtLanTiep.Text = obj.LanTiep.ToString();
            string LanhDao = "";
            for (int i = 0; i < obj.LanhDaos.Count - 1; i++)
            {
                LanhDao += obj.LanhDaos[i].TenLanhDao + ",";
            }
            if (obj.LanhDaos.Count > 0)
                LanhDao += obj.LanhDaos[obj.LanhDaos.Count - 1].TenLanhDao;
            txtLanhDao.Text = LanhDao; //(obj.LanhDaos.Count > 0) ? obj.LanhDaos[0].TenLanhDao : "";
            txtDiaChi.Text = obj.DiaChi;
            txtTomTat.Text = obj.NoiDung;
            txtYKienTiepDan.Text = obj.YKLD;
            // For Dia ban
            try
            {
                this.drpDiaBan.Text = obj.DiaBan.TenDiaBan;
            }catch{
            }
            this.drpDiaBan.ReadOnly = true;
            this.hidDiaBan.Value = Convert.ToString(obj.MaDiaBan);
            // For Loai don
            ListItem item;
            this.drpLoaiHinh.DataSource = this.LoaiHinhList;
            drpLoaiHinh.DataTextField = "TenLoaiHinh";
            drpLoaiHinh.DataValueField = "ID";
            this.drpLoaiHinh.DataBind();
            if (obj.LoaiHinhLookup != null)
            {
                item = drpLoaiHinh.Items.FindByValue(obj.LoaiHinhLookup.ID.ToString());
                if (item != null) item.Selected = true;
            }
            // For Linh vuc
            this.drpLinhVuc.DataSource = this.LinhVucList;
            drpLinhVuc.DataTextField = "TenLinhVuc";
            drpLinhVuc.DataValueField = "ID";
            this.drpLinhVuc.DataBind();
            if (obj.LinhVucLookup != null)
            {
                item = drpLinhVuc.Items.FindByValue(obj.LinhVucLookup.ID.ToString());
                if (item != null) item.Selected = true;
            }

            // For phong ban tiep
            this.drpPBTiepCD.DataSource = this.PBTiepCDList;
            drpPBTiepCD.DataTextField = "TenPB";
            drpPBTiepCD.DataValueField = "ID";
            this.drpPBTiepCD.DataBind();
            if (obj.PBTiepLookup != null)
            {
                item = drpPBTiepCD.Items.FindByValue(obj.PBTiepLookup.ID.ToString());
                if (item != null) item.Selected = true;
            }

            txtKetQuaGiaiQuyet.Text = obj.KetQuaGiaiQuyet;

            item = drpBPTiep.Items.FindByValue(obj.BoPhanTiep.ToString());
            if (item != null) item.Selected = true;
        }
    }
}